<?php
/**
 *
 * Author: Lyu
 * Date: 2023/7/25
 * Time: 15:29
 */

namespace App\OpenApi\AdminApi\Sys;

use App\Service\Core\Sys\Dict\SysDictListService;
use App\Service\Core\Sys\Dict\SysDictService;
use App\Service\Core\Sys\Dict\SysDictStoreService;
use App\Service\OpenApi\AdminApi\OpenBaseApi;

class SysDictApi extends OpenBaseApi
{
    /**
     * 获取字典列表
     * @return array
     */
    public function index(): array
    {
        $list_res = (new SysDictListService())
            ->setFields(['*', 'dict_item_name'])
            ->setDictItem($this->input('dict_item'))
            ->setDictSubVal($this->input('dict_sub_val'))
            ->setPage($this->input('page', 1), $this->input('pageSize', $this->page_size))
            ->getList();
        return alert_info(0, '获取成功', $list_res);
    }

    /**
     * 更新OR修改字典
     * @return array
     */
    public function store(): array
    {
        return (new SysDictStoreService())
            ->setDictItem($this->input('dict_item'))
            ->setDictSubVal($this->input('dict_sub_val'))
            ->setDictSubValExt($this->input('dict_sub_val_ext'))
            ->setSortOrder($this->input('sort_order'))
            ->setStatus($this->input('status'))
            ->setOperate($this->operate_id, $this->operate_by)
            ->store($this->input('id', 0));
    }

    /**
     * 获取字典项
     * @return array
     */
    public function item(): array
    {
        return alert_info(0, '获取成功', SysDictService::getInstance()->getItemTreeList());
    }

    /**
     * 获取指定分类下全部字典
     * @return array
     */
    public function allByItem(): array
    {
        return alert_info(0, '获取成功', SysDictService::getInstance()->getDictAll($this->input('dict_item')));
    }
}
